from zope.interface import implements
import os, sys

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watcher.exceptions import *
from watcher import WatcherContainer, WatcherObject
from watcher.interfaces import IAction, IRunBehavior, IWorkItem
from watcher.config.classbuilder import KlassBuilder


class Action(WatcherContainer):
    """actions are handled by transactions
    """
    implements(IAction)
    def __init__(self, *args, **kwargs):
        super(Action, self).__init__(*args, **kwargs)
        self.retries = self.config.retries.xml_text_content().strip()
        run_behavior = "run_behavior_" + self.config.run_behavior.xml_text_content().strip()
        self.run = KlassBuilder(run_behavior, self.configPath)()(run_behavior, self.configPath)
        self.args = {}
        if not IRunBehavior.providedBy(self.run):
            raise WatcherActionError("run behavior is invalid for %s" %
                                      self.config.name.xml_text_content())


class RunBehavior(WatcherObject):
    """the only requirement here is that __call__
       implement a kind of action.run.
       examples would be:
           copy, move, prepend/append data, extract sub pages, etc
    """
    implements(IRunBehavior)

    def __call__(self):
        raise NotImplementedError("RunBehavior is abstract.  Subclass.")
    



class WorkItem(object):
    """an object representing a work unit, such as a file, blob, serialized
       data, xml string, etc
    """
    implements(IWorkItem)
